package general;

import java.util.Comparator;

public class Functional_Unit extends Device{
    
    //
    //default constructor
    //
    public Functional_Unit () {
        setM(4);
        setDim(5);
        setNumRows(5);
        createCellfield();
    }

    //
    //regular constructor
    //
    protected Functional_Unit (int total_bits, int granularity) {
        setM(granularity);
        setDim(total_bits / granularity);
        setNumRows(total_bits / granularity);
        createCellfield();
    }
    
    //
    //determine the total number of bits required for the new multiplier
    //
    public int smartSize(int A, int B) {
        int nbits = 0; //the number of bits required to represent both operands

        setM(default_granularity); //use the default value for m

        while( ((int)Math.pow(2, nbits) - 1 < A) || 
                ((int)Math.pow(2, nbits) - 1 < B) ) {
                    nbits++;
                }

        setDim(nbits / default_granularity);
        if(nbits % default_granularity != 0) {
            incDim();
        }
        
        return 0;
    }
    
    ///////////////////////////////////////////////////////////////////////////
    //  The following methods may be overridden depending on the type of
    //  functional unit they implement.
    ///////////////////////////////////////////////////////////////////////////
    
    //
    //establish the cell layout
    //
    public void layout() {
    }
   
    //
    //perform the operation
    //
    public int compute() {
        return 0;
    }
    
}

class FUComparator implements Comparator<Functional_Unit> {
   
    //implements
    public int compare(Functional_Unit fua, Functional_Unit fub) {
        if(fua.getDim() < fub.getDim()) {
            return 1;
        }
        else if(fua.getDim() > fub.getDim()) {
            return -1;
        }
        else { //same dimension
            return 0;
        }
    }
    
}
